package com.matrix.draft.util;

/**
 * 算法优化版JTS工具类
 */
public class OptimizedJtsUtil {

    public static final GeometryFactory GEOMETRY_FACTORY = new GeometryFactory(new PrecisionModel(), 4375);

    public static Polygon createCircle2(double x, double y, double radius) {
        final int SIDES = 36;
        final double angle = 2 * Math.PI / SIDES;
        Coordinate[] coords = new Coordinate[SIDES + 1];
        for (int i = 0; i < SIDES; i++) {
            double angleAlpha = i * angle;
            double dx = Math.cos(angleAlpha) * radius + x;
            double dy = Math.sin(angleAlpha) * radius + y;
            coords[i] = new Coordinate(dx, dy);
        }
        coords[SIDES] = coords[0];
        return GEOMETRY_FACTORY.createPolygon(coords);
    }

    public static Polygon createCircle(double x, double y, final double RADIUS) {
        final int SIDES = 32;//圆上面的点个数
        Coordinate coords[] = new Coordinate[SIDES + 1];
        for (int i = 0; i < SIDES; i++) {
            double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;
            double dx = Math.cos(angle) * RADIUS;
            double dy = Math.sin(angle) * RADIUS;
            coords[i] = new Coordinate((double) x + dx, (double) y + dy);
        }
        coords[SIDES] = coords[0];
        LinearRing ring = GEOMETRY_FACTORY.createLinearRing(coords);
        return GEOMETRY_FACTORY.createPolygon(ring, null);
    }

    public static Polygon createCircle2(double x, double y, double radius, int density) {
        final int SIDES = density;
        final double angle = 2 * Math.PI / SIDES;
        Coordinate[] coords = new Coordinate[SIDES + 1];
        for (int i = 0; i < SIDES; i++) {
            double angleAlpha = i * angle;
            double dx = Math.cos(angleAlpha) * radius + x;
            double dy = Math.sin(angleAlpha) * radius + y;
            coords[i] = new Coordinate(dx, dy);
        }
        coords[SIDES] = coords[0];
        return GEOMETRY_FACTORY.createPolygon(coords);
    }

}
